package com.sgb.kjwl.view.ui.activity.enterprise;

import static com.swgk.core.event.EventConstant.EVENT_SERVICE_SEARCH;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityServiceSearchBinding;
import com.sgb.kjwl.view.event.SearchEvent;
import com.sgb.kjwl.view.ui.adapter.CommPagerAdapter3;
import com.sgb.kjwl.view.ui.adapter.home.TabFragmentStateAdapter;
import com.sgb.kjwl.view.ui.fragment.enterprise.PrMakeOverFragment;
import com.sgb.kjwl.view.ui.fragment.enterprise.PrNeedFragment;
import com.sgb.kjwl.view.ui.fragment.enterprise.PrProfessionalTeamFragment;
import com.sgb.kjwl.view.ui.fragment.enterprise.SearchServiceFragment;
import com.sgb.kjwl.view.widget.ActionBar;
import com.swgk.core.helper.OnMultiClickListener;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SoftKeyBoardUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

/**
 * 企业服务搜索页面
 */
public class ServiceSearchActivity extends AppActivity {

    private ActivityServiceSearchBinding mBinding;
    private final String[] tagName = {"服务", "需求", "知产", "专业团队"};
    private TabFragmentStateAdapter mTabFragmentStateAdapter;
    private ArrayList<Fragment> fragmentList = new ArrayList<>();

    public static void start(Context mContext) {
        Intent mIntent = new Intent();
        mIntent.setClass(mContext, ServiceSearchActivity.class);
        mContext.startActivity(mIntent);
    }


    @Override
    protected void initView() {
        super.initView();
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_service_search);
        mBinding.setLifecycleOwner(this);
        initFragmentAdapter();
        initSearch();
    }

    private void initFragmentAdapter() {
        fragmentList.add(SearchServiceFragment.newInstance());
        fragmentList.add(PrNeedFragment.newInstance());
        fragmentList.add(PrMakeOverFragment.newInstance());
        fragmentList.add(PrProfessionalTeamFragment.newInstance());
        if (mTabFragmentStateAdapter == null) {
            mTabFragmentStateAdapter = new TabFragmentStateAdapter(this, fragmentList);
        }
        CommPagerAdapter3 pagerAdapter = new CommPagerAdapter3(getSupportFragmentManager(), fragmentList, tagName);
        mBinding.viewPager.setOffscreenPageLimit(tagName.length);
        mBinding.viewPager.setAdapter(pagerAdapter);
        mBinding.tabLayout.setupWithViewPager(mBinding.viewPager);
        mBinding.tabLayout.getTabAt(0).select();
    }

    private void initSearch() {
        SearchEvent searchEvent = new SearchEvent();
        searchEvent.setMessageTag(EVENT_SERVICE_SEARCH);
        mBinding.actionBar.openSoftInput();
        mBinding.actionBar.setClearEdiText(new ActionBar.ClearEdiText() {
            @Override
            public void clearEdiText() {
//                SoftKeyBoardUtil.hideSoftKeyboard(ServiceSearchActivity.this);
//                searchEvent.setMessageData("");
//                EventBus.getDefault().postSticky(searchEvent);
                mBinding.actionBar.openSoftInput();
                mBinding.rvNoSearch.setVisibility(View.VISIBLE);
            }

            @Override
            public void search() {
                if (TextUtils.isEmpty(mBinding.actionBar.getEditText().trim())) {
                    MToast.showToast(ServiceSearchActivity.this, "请输入关键字");
                    mBinding.actionBar.setEditSearch("");
                    mBinding.actionBar.openSoftInput();
                    return;
                }
                postSearch(searchEvent);
            }
        });
        mBinding.actionBar.setMenuFunctionClick(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                if (TextUtils.isEmpty(mBinding.actionBar.getEditText().trim())) {
                    MToast.showToast(ServiceSearchActivity.this, "请输入关键字");
                    mBinding.actionBar.setEditSearch("");
                    mBinding.actionBar.openSoftInput();
                    return;
                }
                SoftKeyBoardUtil.hideSoftKeyboard(ServiceSearchActivity.this);
                postSearch(searchEvent);
            }
        });
    }


    private void postSearch(SearchEvent searchEvent) {
        mBinding.rvNoSearch.setVisibility(View.GONE);
        searchEvent.setMessageData(mBinding.actionBar.getEditText());
        EventBus.getDefault().postSticky(searchEvent);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}
